﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Item : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack) return;
        int id;
        if (!int.TryParse(Request.QueryString["id"], out id))
            id = 1;
        ItemBLL it = new ItemBLL();
        ItemDetail.DataSource = it.getItem_Details(id);
        ItemDetail.DataBind();
    }
    protected bool addToCart(string id)
    {
        bool status = false;
        string cart = null;
        if (Session["cart"] == null)
        {
            cart += id + ",";
            Session["cart"] = cart;
            status = true;
        }
        else
        {
            if (Session["item" + id] == null)
            {
                cart = Session["cart"].ToString();
                cart += id + ",";
                Session["cart"] = cart;
            }
            status = true;
        }
        
        if (Session["item" + id] == null)
        {
            Session["number"] = 1;
            Session["item" + id] = 1;
            int num = Int32.Parse(Session["number"].ToString());
            num += 1;
            Session["number"] = num;
        }
        else
        {
            int count = Int32.Parse(Session["item" + id].ToString());
            count += 1;
            Session["item" + id] = count;
        }

        return status;
    }
    protected void ItemDetail_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "addItem")
        {
            if (addToCart(e.CommandArgument.ToString()))
                Response.Redirect("Cart.aspx");
        }
    }
    protected void BtnAddItem_Click(object sender, EventArgs e)
    {

    }
}